package howe.sudoku.db;

import howe.sudoku.entity.SudokuInfoVo;
import howe.sudoku.names.SudokuName;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class SudokuDao extends DatabaseHelper {

    SudokuDao(Context context) {
        super(context);
    }

    public List<SudokuInfoVo> getSudokuInfoList(long folderID) {
        Cursor cursor = null;
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        List<SudokuInfoVo> ret = null;
        try {
            qb.setTables(SudokuSimpleSQL.SUDOKU_TABLE_NAME);
            qb.appendWhere(SudokuSimpleSQL.FOLDER_ID + "=" + folderID);
            cursor = qb.query(db, null, null, null, null, null, "created DESC");
            if (0 < cursor.getCount()) {
                ret = new ArrayList<SudokuInfoVo>();
                while (cursor.moveToNext()) {
                    SudokuInfoVo item = new SudokuInfoVo();
                    item.setId(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL._ID)));
                    item.setFolderId(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.FOLDER_ID)));
                    item.setCreated(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.CREATED)));
                    item.setState(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.STATE)));
                    item.setTime(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.TIME)));
                    item.setLastPlayed(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.LAST_PLAYED)));
                    item.setData(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.DATA)));
                    item.setReadOnlyData(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.READ_ONLY_DATA)));
                    item.setNote(cursor.getString(cursor.getColumnIndex(SudokuSimpleSQL.PUZZLE_NOTE)));
                    ret.add(item);
                }
            }
        } finally {
            closeCursor(cursor);
        }
        Log.i(SudokuName.DBTAG, "Select SudokuInfoList");
        return ret;
    }

    /**
     * Updates sudoku game in the database.
     *
     * @param input
     */
    public void updateSudoku(SudokuInfoVo input) {
        Log.i(SudokuName.DBTAG, "Update Sudoku");
        SQLiteDatabase db = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(SudokuSimpleSQL.DATA, input.getData());
        values.put(SudokuSimpleSQL.LAST_PLAYED, input.getLastPlayed());
        values.put(SudokuSimpleSQL.STATE, input.getState());
        values.put(SudokuSimpleSQL.TIME, input.getTime());
        values.put(SudokuSimpleSQL.PUZZLE_NOTE, input.getNote());
        db.update(SudokuSimpleSQL.SUDOKU_TABLE_NAME, values, SudokuSimpleSQL._ID + "=" + input.getId(), null);
    }

    /**
     * Returns sudoku game object.
     *
     * @param sudokuID Primary key of folder.
     * @return
     */
    public SudokuInfoVo getSudoku(long sudokuID) {
        Log.i(SudokuName.DBTAG, "Select Sudoku");
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        qb.setTables(SudokuSimpleSQL.SUDOKU_TABLE_NAME);
        qb.appendWhere(SudokuSimpleSQL._ID + "=" + sudokuID);

        // Get the database and run the query
        SQLiteDatabase db = null;
        Cursor c = null;
        SudokuInfoVo s = null;
        try {
            db = getReadableDatabase();
            c = qb.query(db, null, null, null, null, null, null);

            if (c.moveToFirst()) {
                long id = c.getLong(c.getColumnIndex(SudokuSimpleSQL._ID));
                long folderId = c.getLong(c.getColumnIndex(SudokuSimpleSQL.FOLDER_ID));
                long created = c.getLong(c.getColumnIndex(SudokuSimpleSQL.CREATED));
                String data = c.getString(c.getColumnIndex(SudokuSimpleSQL.DATA));
                String readOnlyData = c.getString(c.getColumnIndex(SudokuSimpleSQL.READ_ONLY_DATA));
                long lastPlayed = c.getLong(c.getColumnIndex(SudokuSimpleSQL.LAST_PLAYED));
                int state = c.getInt(c.getColumnIndex(SudokuSimpleSQL.STATE));
                long time = c.getLong(c.getColumnIndex(SudokuSimpleSQL.TIME));
                String note = c.getString(c.getColumnIndex(SudokuSimpleSQL.PUZZLE_NOTE));

                s = new SudokuInfoVo();
                s.setId(String.valueOf(id));
                s.setFolderId(String.valueOf(folderId));
                s.setCreated(String.valueOf(created));
                s.setData(data);
                s.setReadOnlyData(readOnlyData);
                s.setLastPlayed(String.valueOf(lastPlayed));
                s.setState(String.valueOf(state));
                s.setTime(String.valueOf(time));
                s.setNote(note);
            }
        } finally {
            closeCursor(c);
        }
        return s;
    }
}